//
//  DYGroupingResultsViewController.m
//  DaYing
//
//  Created by DaYing on 2024/1/14.
//  Copyright © 2024 DaYing. All rights reserved.
//

#import "DYGroupingResultsViewController.h"

#import "DYGroupingResultsCell.h"
#import "DYGroupingResultsContentCell.h"
#import "DYGroupingResultsTitleCell.h"
#import "DYLotteryDecisionButtonCell.h"

#import "DYGroupSettingsViewController.h"
#import "DYCreateSignatureToolBar.h"

@interface DYGroupingResultsViewController ()

@property (nonatomic,strong) NSMutableArray *mynewAry;
@property (nonatomic,strong) DYCreateSignatureToolBar *toolBar; //提交按钮bar

@end

@implementation DYGroupingResultsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setNavigationItemTitleViewWithTitle:@"分组结果"];

    [self.tableView registerNib:[UINib nibWithNibName:@"DYGroupingResultsCell" bundle:nil] forCellReuseIdentifier:@"DYGroupingResultsCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"DYGroupingResultsContentCell" bundle:nil] forCellReuseIdentifier:@"DYGroupingResultsContentCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"DYGroupingResultsTitleCell" bundle:nil] forCellReuseIdentifier:@"DYGroupingResultsTitleCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"DYLotteryDecisionButtonCell" bundle:nil] forCellReuseIdentifier:@"DYLotteryDecisionButtonCell"];
    
    [self.view addSubview:self.toolBar];
    
    self.tableView.frame = CGRectMake(0,self.navigationBarHeight, kScreenWidth, kScreenHeight - self.navigationBarHeight - self.toolBar.frame.size.height);
    [self.view bringSubviewToFront:self.navigationBar];
}

- (void)setGroupList:(NSArray *)groupList {
    _groupList = groupList;
    _mynewAry = [NSMutableArray arrayWithArray:_groupList];
}

#pragma mark - cellDataSource

- (NSArray *)cellDataSource {
    
    if (!self.dataSource) {
        NSMutableArray *arr = [NSMutableArray arrayWithCapacity:4];
        
        __block NSMutableArray *subarr = nil;
        __block NSDictionary *dict = nil;
        
        subarr = [NSMutableArray arrayWithCapacity:1];
        
        dict =  @{@"class":DYGroupingResultsCell.class,
                  @"height":@([DYGroupingResultsCell getCellFrame:[DYGroupingResultsCellModel ittemModelWithName:_name count:_count groupList:_groupList]]),
                  @"data":[DYGroupingResultsCellModel ittemModelWithName:_name count:_count groupList:_groupList]};
        [subarr addObject:dict];
        
        dict =  @{@"class":DYGroupingResultsTitleCell.class,
                  @"height":@([DYGroupingResultsTitleCell getCellFrame:[NSNumber numberWithFloat:72]]),
                  @"data":[DYGroupingResultsTitleCellModel ittemModelWithTitle:@"分组结果"]};
        [subarr addObject:dict];
        
        NSMutableArray *strAry = [NSMutableArray arrayWithCapacity:_count.integerValue];
        
        for (int i = 0; i < _count.integerValue; i++) {
            int k = arc4random() % _mynewAry.count;
            NSString *n = _mynewAry[k];
            [strAry addObject:[NSString stringWithFormat:@"第%d组：%@",i+1,n]];
            [_mynewAry removeObject:n];
        }
        
        [_mynewAry enumerateObjectsUsingBlock:^(NSString *str, NSUInteger idx, BOOL * _Nonnull stop) {
            int k = arc4random() % strAry.count;
            NSString *string = strAry[k];
            [strAry replaceObjectAtIndex:k withObject:[NSString stringWithFormat:@"%@,%@",string,str]];
        }];
        
        [strAry enumerateObjectsUsingBlock:^(NSString *s, NSUInteger idx, BOOL * _Nonnull stop) {

            NSInteger position = 0;
            if (strAry.count == 1) {
                position = 3;
            } else if (idx + 1 == strAry.count) {
                position = 2;
            } else if (idx > 0) {
                position = 1;
            }

            dict =  @{@"class":DYGroupingResultsContentCell.class,
                      @"height":@([DYGroupingResultsContentCell getCellFrame:[DYGroupingResultsContentCellModel ittemModelWithTitle:s position:position]]),
                      @"data":[DYGroupingResultsContentCellModel ittemModelWithTitle:s position:position]};
            [subarr addObject:dict];
        }];
             
        [arr addObject:subarr];

        self.dataSource = arr;
    }
    return self.dataSource;
}

- (void)backAction:(UIButton *)sender {
    [self.navigationController popToRootViewControllerAnimated:YES];
}

#pragma mark - goto

//新建分组
- (void)gotoGroupSettingsViewController {
    DYGroupSettingsViewController *vc = [DYGroupSettingsViewController new];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - 懒加载

//底部工具条
- (DYCreateSignatureToolBar *)toolBar {
    if (!_toolBar) {
        _toolBar = [DYCreateSignatureToolBar instanceView];
        [_toolBar.topButton setTitle:@"重新分组" forState:UIControlStateNormal];
        [_toolBar.bottomButton setTitle:@"新建分组" forState:UIControlStateNormal];
        _toolBar.frame = CGRectMake(0,kScreenHeight - 200, kScreenWidth,200);
        @weakify(self)
        _toolBar.buttonAction = ^(DYCreateSignatureToolBar *toolBar, UIButton *sender) {
            @strongify(self);
            if (sender.tag == 10) {
                //重新分组
                _mynewAry = [NSMutableArray arrayWithArray:_groupList];
                [self refreshData];
            } else if (sender.tag == 11) {
                //新建分组
                [self gotoGroupSettingsViewController];
            }
        };
        [self.view addSubview:_toolBar];
    }
    return _toolBar;
}

@end
